package com.zkjl.protect_business.controller;

import com.zkjl.protect_business.common.ApiResponse;
import com.zkjl.protect_business.common.SystemLog;
import com.zkjl.protect_business.dto.OrganizationDTO;
import com.zkjl.protect_business.entity.Organization;
import com.zkjl.protect_business.repository.OrganizationRepository;
import com.zkjl.protect_business.service.OrganizationService;
import com.zkjl.protect_business.vo.OrganizationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/organizations")
@Api(tags = {"单位"})
public class OrganizationController {

    @Autowired
    OrganizationService organizationService;
    @Autowired
    OrganizationRepository organizationRepository;


    @ApiOperation(value = "新增单位", notes = "新增单位", httpMethod = "POST")
    @PostMapping
    @SystemLog(description = "新增单位")
    public ApiResponse<Organization> save(@RequestBody @Validated OrganizationDTO organizationDTO) {

        Organization organization = organizationService.saveOrUpdate(organizationDTO, null);
        return ApiResponse.ofSuccess(organization);

    }

    @ApiOperation(value = "修改单位", notes = "修改单位", httpMethod = "PUT")
    @PutMapping(value = "{id}")
    @SystemLog(description = "修改单位")
    public ApiResponse<Organization> save(@RequestBody OrganizationDTO organizationDTO, @PathVariable String id) {

        Organization organization = organizationService.update(organizationDTO, id);
        return ApiResponse.ofSuccess(organization);

    }

    @ApiOperation(value = "获取单位树形列表", httpMethod = "GET")
    @GetMapping(value = "tree")
    public ApiResponse<List<OrganizationVO>> getOrganizationTree() {

        List<OrganizationVO> organizationTree = organizationService.getOrganizationTree();
        return ApiResponse.ofSuccess(organizationTree);
    }

    @ApiOperation(value = "获取单位详情", httpMethod = "GET")
    @GetMapping(value = "{id}")
    public ApiResponse<Organization> get(@PathVariable String id) {
        Organization organization = organizationService.get(id);
        return ApiResponse.ofSuccess(organization);
    }

    @ApiOperation(value = "获取单位列表", httpMethod = "GET")
    @GetMapping
    public ApiResponse<List<Organization>> list() {
        List<Organization> list = organizationService.list();
        return ApiResponse.ofSuccess(list);
    }

    @ApiOperation(value = "获取单位的所有下级单位", httpMethod = "GET")
    @GetMapping(value = "{id}/childrens")
    public ApiResponse<List<Organization>> list(@PathVariable String id) {
        List<Organization> organizations = organizationService.listChildrens(id);
        Organization organization = organizationService.get(id);
        organizations.add(organization);

        return ApiResponse.ofSuccess(organizations);
    }


    @ApiOperation(value = "删除单位", httpMethod = "DELETE")
    @SystemLog(description = "删除单位")
    @DeleteMapping(value = "{id}")
    public ApiResponse delete(@PathVariable String id) {

        Boolean del = organizationService.del(id);

        return ApiResponse.ofSuccess(del);
    }


}
